import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
// Vite3 需要 Node.js 版本 14.18+，16+
import AutoImport from "unplugin-auto-import/vite"
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import path from 'path';

export default defineConfig(({ mode }) => {
  // 获取配置环境
  const env = loadEnv(mode, process.cwd(), '')
  return {
    plugins: [
      vue(),
      // 自动导入的模块
      AutoImport({
        imports: ['vue', 'vue-router', 'vuex'],
        // 自定义文件
        // dir: ['./src/api'],
        resolvers: [ElementPlusResolver()],
        dts: './src/auto-imports.d.ts'
      }),
      Components({
        resolvers: [ElementPlusResolver()],
        dts: './src/components.d.ts',
      }),
    ],
    base: './',
    resolve: {
      alias: [
        { find: '@', replacement: path.resolve(__dirname, 'src') },
      ],
    },
    server: {
      host: "0.0.0.0",
      // 代理转发
      proxy: {
        "/APIMAPBAIDU": {
          target: 'http://api.map.baidu.com',
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/APIMAPBAIDU/, ''),
        }
      }
    },
    build: {
      /**
       * @console [development, beta]
       * @debugger [development]
       * @production 全部关闭
       */
      minify: 'terser',
      terserOptions: {
        compress: {
          drop_console: env.VITE_NODE_ENV == 'production',
          drop_debugger: env.VITE_NODE_ENV != 'development',
        },
      },
    }
  }
})
